package com.yanyeori.webtest.service.impl;

import com.yanyeori.framework.datasource.mybatisplus.service.impl.ServiceImpl;
import com.yanyeori.webtest.entity.clear.TestData;
import com.yanyeori.webtest.mapper.TestDataMapper;
import com.yanyeori.webtest.service.TestDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenkuan
 * @date 2023/8/28
 */
@Slf4j
@Service
public class TestDataServiceImpl extends ServiceImpl<TestDataMapper, TestData> implements TestDataService {

    private int from = 0;

    @Override
    public void saveBatch(int size) {
        List<TestData> list = new ArrayList<>();
        TestData entity;
        for (int i = from, j = from + size; i < j; i++, from++) {
            entity = new TestData();
            entity.setPrimaryKey1("primaryKey1_saveBatch_" + i);
            entity.setPrimaryKey2("primaryKey2_saveBatch_" + i);
            entity.setTest1("test1_" + i);
            entity.setTest2("test2_" + i);
            entity.setTest3("test3_" + i);
            entity.setTest4("test4_" + i);
            entity.setTest5("test5_" + i);
            list.add(entity);
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        saveBatch(list);
        stopWatch.stop();
        log.info("saveBatch, size: {}, time: {}ms", list.size(), stopWatch.getLastTaskTimeMillis());
    }

    @Override
    public void insertBatch(int size) {
        List<TestData> list = new ArrayList<>();
        TestData entity;
        for (int i = from, j = from + size; i < j; i++, from++) {
            entity = new TestData();
            entity.setPrimaryKey1("primaryKey1_insertBatch_" + i);
            entity.setPrimaryKey2("primaryKey2_insertBatch_" + i);
            entity.setTest1("test1_" + i);
            entity.setTest2("test2_" + i);
            entity.setTest3("test3_" + i);
            entity.setTest4("test4_" + i);
            entity.setTest5("test5_" + i);
            list.add(entity);
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        insertBatch(list, 1000);
        stopWatch.stop();
        log.info("insertBatch, size: {}, time: {}ms", list.size(), stopWatch.getLastTaskTimeMillis());
    }
}
